package com.hua.yuan.live.repertory.p2p;
import android.text.TextUtils;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.hua.yuan.live.repertory.cloud_data.res.GroupChat;
import com.hua.yuan.rtcroom.RTCRoom;
import com.hua.yuan.rtcroom.sdk.RoomInfo;
import com.hua.yuan.rtcroom.sdk.im.IMMessageMgr;

import java.util.ArrayList;

import static com.tencent.rtmp.TXLivePlayer.TAG;

/**
 * @author richsjeson
 * P2P 点对点电话
 */
public class LiveP2pCreateRoom {


    private static RTCRoom mRtcRoom;

    private static String mCreateRoomId;


    public interface IMessageCallBack{
        void startPush();
        void onError(int errCode, String errInfo);
        void onSuccess();
    }

    private static IMessageCallBack mCallback;


    public void setCallback(IMessageCallBack pCallback){

        this.mCallback=pCallback;
    }

    public void setRTCRomm(RTCRoom pRtcRoom){

        this.mRtcRoom=pRtcRoom;
    }

    public void setCreateRoomId(String pCreateRoomId){

        this.mCreateRoomId=pCreateRoomId;
    }
    /**
     * 创建房间
     */
    public void start() {

        if (TextUtils.isEmpty(mCreateRoomId)) {
            return;
        }
        mRtcRoom.initLivePusher();
        mRtcRoom.getRoomList(0,10,getRoomListCallback);

    }

    private static RTCRoom.GetRoomListCallback getRoomListCallback=new RTCRoom.GetRoomListCallback() {
        @Override
        public void onError(int errCode, String errInfo) {
            mCallback.startPush();
            //拿到主播的ID后，以主播的rtc_user_id为主，创建房间
            mRtcRoom.createRoom(mCreateRoomId, "",createRoomCallback);
        }

        @Override
        public void onSuccess(ArrayList<RoomInfo> roomInfoList) {
            mCallback.startPush();
            if(roomInfoList.size()==0 ){
                //拿到主播的ID后，以主播的rtc_user_id为主，创建房间
                mRtcRoom.createRoom(mCreateRoomId, "",createRoomCallback);
                return;
            }
            if(!isOpen(roomInfoList,mCreateRoomId)){
                mRtcRoom.createRoom(mCreateRoomId, "",createRoomCallback);
                return;
            }
            sendMessage();
        }
    };


    private static boolean isOpen(ArrayList<RoomInfo> roomInfoList, String pRoomId){

        boolean isCreateRoomId=false;

        for(int i=0;i<roomInfoList.size();i++){
            if(isRTCRoomOpen(roomInfoList,i,pRoomId)){
                isCreateRoomId=true;
                break;
            }
        }
        return isCreateRoomId;
    }


    private static boolean isRTCRoomOpen(ArrayList<RoomInfo> roomInfoList,int index,String pRoomId){

        RoomInfo roomInfo=roomInfoList.get(index);
        if(roomInfo==null){
            return false;
        }
        if(roomInfo.roomID.equals(pRoomId)){
            return true;
        }
        return false;
    }

    private static RTCRoom.CreateRoomCallback createRoomCallback=new RTCRoom.CreateRoomCallback() {
        @Override
        public void onError(int errCode, String errInfo) {

            if(errCode==11){
                sendMessage();
                return;
            }
            mCallback.onError(errCode, errInfo);
        }

        @Override
        public void onSuccess(String name) {
            sendMessage();
        }
    };


    private static void sendMessage() {
        //发送消息给主播，并告知进入该房间
        GroupChat groupChat = new GroupChat();
        groupChat.setType(1);
        groupChat.setContent(null);
        groupChat.setRoom(mCreateRoomId);
        groupChat.setUserName(null);
        String content = JSON.toJSONString(groupChat);
        mRtcRoom.getIMMessageMgr().sendC2CCustomMessage(mCreateRoomId, content, messageCallback);
    }

    private static IMMessageMgr.Callback messageCallback=new IMMessageMgr.Callback() {
        @Override
        public void onError(int code, String errInfo) {

            mCallback.onError(code,errInfo);
        }

        @Override
        public void onSuccess(Object... args) {
            Log.i(TAG, "发送给主播的信息，发送成功了");
            mRtcRoom.enterRoom(mCreateRoomId, enterRoomCallback);
        }
    };

    private static RTCRoom.EnterRoomCallback enterRoomCallback=new RTCRoom.EnterRoomCallback() {

        @Override
        public void onError(int errCode, String errInfo) {
            mCallback.onError(errCode,errInfo);
            Log.i(TAG, "errInfo："+errInfo);
        }

        @Override
        public void onSuccess() {
            mCallback.onSuccess();
        }
    };
}
